package com.wust.loveangel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.wust.R;
import com.wust.data.KnowledgeMap;
import com.wust.data.MapDao;
import com.wust.footprint.FirstActivity;
import com.wust.footprint.diaryList;
import com.wust.menu.FirstGroupTab;
import com.wust.menu.ThirdGroupTab;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class loveAngelActivity2 extends ListActivity {
	private ListView listview=null;
	private ArrayList<HashMap<String, Object>> listItems;
	private TextView loveangel_title=null;
	private Button loveangel_back=null;
	private MapDao mapDao;
	private List<KnowledgeMap>maplist;
	private String id2;
	private String name=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.loveangel2);
		loveangel_back=(Button)findViewById(R.id.loveangel_back);
		loveangel_title=(TextView)findViewById(R.id.loveangel_title);
		mapDao=new MapDao(this);
		Intent intent=getIntent();
		id2=intent.getStringExtra("msg");
		KnowledgeMap kmap=mapDao.getMapById(id2);
		name=kmap.getCat_name();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		loveangel_back.setOnClickListener(new OnListener());
		loveangel_title.setText(name);
		listview=getListView();
		listview.setAdapter(buildAdapter());	
		super.onResume();
	}
	private  SimpleAdapter buildAdapter(){
		listItems = new ArrayList<HashMap<String, Object>>();   
		maplist=mapDao.queryMapByparentId(id2);
		for (Iterator iterator = maplist.iterator(); iterator.hasNext();) {
			KnowledgeMap kmap = (KnowledgeMap) iterator.next();
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("loveangel_item_title", kmap.getCat_name());
			listItems.add(map);
		}      
        //生成适配器的Item和动态数组对应的元素       
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItems,//数据源        
            R.layout.loveangel_first_item,//ListItem的XML布局实现       
            //动态数组与ImageItem对应的子项               
            new String[] {"loveangel_item_title"},        
            //ImageItem的XML文件里面的一个ImageView,两个TextView ID       
            new int[] {R.id.loveangel_item_title}       
        ); 
        return listItemAdapter;
	}
	private void buildList(){
		listview.setAdapter(buildAdapter());
	}
	@Override  
    public void onBackPressed() {   
        //把后退事件交给子Activity处理  
		 back();
    } 
	@Override
	protected void onListItemClick(ListView l, View v, int position, long ids) {
		// TODO Auto-generated method stub
		
		super.onListItemClick(l, v, position, ids);
		//要跳转的Activity  
		Intent intent=new Intent();
		intent.setClass(this, loveAngelActivity3.class);
		String msg=String.valueOf(maplist.get(position).getId());
		System.out.println("love2:msg="+msg+",id2="+id2);
		jumptoActivity(loveAngelActivity3.class,"loveAngelActivity3",new String[]{"msg","id2"},new String[]{msg,id2});		
	}
	private void jumptoActivity(Class activity,String activityname ,String key[],String value[]){
		Intent intent = new Intent(loveAngelActivity2.this, activity);  
		if(key!=null&&value!=null&&key.length==value.length)
		
			for(int i=0;i<key.length;++i)
				intent.putExtra(key[i], value[i]);
        // 把Activity转换成一个Window，然后转换成View  
        Window w = ThirdGroupTab.group.getLocalActivityManager()  
                .startActivity(activityname,intent);  
        View view = w.getDecorView();  
        //设置要跳转的Activity显示为本ActivityGroup的内容  
        ThirdGroupTab.group.setContentView(view); 
	}
	private void back(){
		Intent intent = new Intent(loveAngelActivity2.this, loveAngelActivity.class);  
        // 把Activity转换成一个Window，然后转换成View  
		intent.putExtra("msg","0");
        Window w = ThirdGroupTab.group.getLocalActivityManager()  
                .startActivity("loveAngelActivity2",intent);  
        View view = w.getDecorView();  
        //设置要跳转的Activity显示为本ActivityGroup的内容  
        ThirdGroupTab.group.setContentView(view); 
	}
    class OnListener implements OnClickListener{
    	@Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		back();
    	}
    }
}
